package com.weibo.game;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.weibo.game.util.HttpUtil;
import com.weibo.game.util.MD5;


/**
 * 接口1.1 用户信息校验接口
 * 
 * http://m.game.weibo.cn/api/sdk/user/check.json
 * 详细见联运SDK服务端校验文档
 */
public class UserCheck {
    
    /** 分配给游戏的appkey */
    public static final String appkey = "3470157549";
    
    /** 分配给游戏进行运算的加密串*/
    public static final String signatureKey ="870eb7bd421fc6d1a7861f3a31ceba35";
    
    /**  客户端调用 WeiboGameSDKHelper.getMachineId(this) 获取当前设备ID */
    public static final String deviceID = "6c392cca2888506e5af35a67892c9725";
    
    /** 客户端登入成功回调的对象中getSuid()获取的sUid */
    public static final String sUid = "200000000010";
    
    /** 客户端登入成功回调的对象中getToken()获取的token */
    public static final String token = "SNG_bopZb0HGB7tm88haBz5OoAb8hfBwlp0";
    
    
    /** 用户信息校验接口 */
    public static final String url = "http://m.game.weibo.cn/api/sdk/user/check.json";
    
    public static void main(String[] args) {
        Map<String,String> params = new HashMap<String,String>();
        params.put("suid", sUid);
        params.put("appkey", appkey);
        params.put("deviceid", deviceID);
        params.put("token", token);
        
        String signature = createUserSignature(params,signatureKey);
        params.put("signature", signature);
        
        try {
            String json = HttpUtil.post(url, params);
            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 计算用户接口签名
     * @param params
     * @param signatureKey
     * @return
     */
    private static String createUserSignature(Map<String,String> params,String signatureKey) {
        String signature = null;
        if(params != null){
            StringBuilder sValue = new StringBuilder();
            Object[] keys = params.keySet().toArray();
            Arrays.sort(keys);
            String temp = null;
            for(Object key : keys){
                sValue.append(key).append("=");
                temp = params.get(key);
                if(temp == null){
                    sValue.append("").append("&");
                }else{
                    sValue.append(temp).append("&");
                }
            }
            if(sValue.length()>0){
                sValue.deleteCharAt(sValue.length() - 1);
                sValue.append("|").append(signatureKey);
            }
            signature = MD5.md5sum(sValue.toString());
        }
        return signature;
    }
    
    
    
}
